#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-

import Tkinter

DEBUT = 0
FIN = 1

class Meter(Tkinter.Frame):
    def __init__(self, master, fillcolor='orchid1', text='', value=0.0, sens = None, **kw):
        Tkinter.Frame.__init__(self, master, bg='white', width=350, height=20)
        self.configure(**kw)
        self._sens = sens
        self._inc = 0.1
        self._c = Tkinter.Canvas(self, bg=self['bg'],
            width=self['width'], height=self['height'],\
            highlightthickness=0, relief='flat',
            bd=0)
        self._c.pack(fill='x', expand=1)
        self._r = self._c.create_rectangle(0, 0, 0, int(self['height']), fill=fillcolor, width=0)
        self._t = self._c.create_text(int(self['width'])/2, int(self['height'])/2, text='')
        
        self.set(value, text)

    def init(self):
        self._sens = None

    def set(self, value=0.0, text=None, sens = None):
        if value < 0.0:
            value = 0.0
        elif value > 1.0:
            value = 1.0
        self._value = value
        if text == None:
            text = str(int(round(100 * value))) + ' %'
        if sens == FIN:
            self._c.coords(self._r, int(self['width']) * value, 0, int(self['width']), int(self['height']))
        else:
            self._c.coords(self._r, 0, 0, int(self['width']) * value, int(self['height']))
        self._c.itemconfigure(self._t, text=text)

    def inc(self):
        if self._sens is not None: 
            if self._value > 1.:
                if self._sens == DEBUT:
                    self._sens = FIN
                    self._value = 0.
                    self._inc = 0.1
                else:
                    self._inc = -0.1
            elif self._value < 0.:
                if self._sens == DEBUT:
                    self._inc = 0.1
                else:
                    self._sens = DEBUT
                    self._value = 1.
                    self._inc = -0.1
        else:
            self._sens = DEBUT
            self._value = 0.
            self._inc = 0.1
        self.set(self._value, '', self._sens)

        self._value += self._inc
